/**
 * 
 */
package com.smarthome.web.property.controller.housemanager;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.housemanager.BuildingManagerService;
import com.smarthome.core.housemanager.HouseDoorplatService;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.domain.bean.ShDoor;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * @author dhc
 * @date 2015年9月14日 下午3:15:11
 * @title 门牌号管理
 */
@Controller
@RequestMapping("houseDoorplatManager")
public class HouseDoorplatController {
	
	private static Logger logger = LoggerFactory.getLogger(HouseDoorplatController.class);
	
	@Resource(name="buildingManagerService")
	private BuildingManagerService buildingManagerService;
	
	@Resource(name="residenceManagerService")
	private ResidenceManagerService residenceManagerService;
	
	@Resource(name="houseDoorplatService")
	private HouseDoorplatService doorplatService;
	private boolean isAdmin= true;
	@RequestMapping("/views")
	public String views(HttpServletRequest request){
		//权限判断，如果是admin
		isAdmin = UserHolder.isSuperResidence();
		request.setAttribute("isAdmin", isAdmin);
		if(isAdmin){
			request.setAttribute("residences", residenceManagerService.queryShResidences(null));
		} else {
			Long residenceId = UserHolder.getCurrentResidenceId();
			ShResidence residence = new ShResidence();
			residence.setId(residenceId);
			//设置当前小区
			request.setAttribute("residence", residence);
			//设置当前楼栋
			request.setAttribute("building", buildingManagerService.queryShBuildingsByResidenceId(residenceId));
		}
		
		return "/housemanager/houseDoorplatManager";
	}
	
	
	
	@RequestMapping("/combobox")
	@ResponseBody
	public List<Combobox> getCombobox(Long buildingUnitId){
		return doorplatService.getComboboxByUnitId(buildingUnitId);
	}
	
	
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShDoor door, PageHelper page){
		return doorplatService.getDataGrid(door, page);
	}
	
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,Long pid) {
		request.setAttribute("residenceId", request.getParameter("residenceId")); //小区
		request.setAttribute("parentId", request.getParameter("parentId"));           //楼栋
		request.setAttribute("buildingId", request.getParameter("buildingId"));   //单元
		isAdmin = UserHolder.isSuperResidence();
		//权限判断，如果是admin
		request.setAttribute("isAdmin", isAdmin);
		if(isAdmin){
			request.setAttribute("residentces", residenceManagerService.queryShResidences(null));
		} else {
			Long residenceId = UserHolder.getCurrentResidenceId();
			ShResidence residence = new ShResidence();
			residence.setId(residenceId);
			//设置当前小区
			request.setAttribute("residence", 	residenceManagerService.queryResidenceById(residenceId));
			request.setAttribute("building", buildingManagerService.queryShBuildingsByResidenceId(residenceId));
		}
		
        return "/housemanager/houseDoorplatAdd";
	}
	@RequestMapping("/editPage")
	public String eidtPage(HttpServletRequest request,Long id) {
		ShDoor door = doorplatService.querydoorById(id);
		request.setAttribute("door", door);
		return "/housemanager/houseDoorplatEdit";
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public Result add(ShDoor door){
		if(StringUtils.isBlank(door.getNum())){
			return Result.failure("单元号不能为空");
		}
		
		if(StringUtils.isBlank(door.getSpace())){
			return Result.failure("建筑面积不能为空");
		}
		
		if(StringUtils.isBlank(door.getComment())){
			return Result.failure("描述不能为空");
		}
		
		//单元重名验证
		if(doorplatService.isRepeat(door)){
			return Result.failure("单元号重名");
		}
		
		
		doorplatService.add(door);
		return Result.success("添加门牌成功");
	}
	
	
	@ResponseBody
	@RequestMapping("/edit")
	public Result edit(ShDoor door){
		if(StringUtils.isBlank(door.getNum())){
			return Result.failure("单元号不能为空");
		}
		
		if(StringUtils.isBlank(door.getSpace())){
			return Result.failure("建筑面积不能为空");
		}
		
		if(StringUtils.isBlank(door.getComment())){
			return Result.failure("描述不能为空");
		}
		
		//单元重名验证
		if(doorplatService.isRepeat(door)){
			return Result.failure("单元号重名");
		}
		doorplatService.edit(door);
		return Result.success("修改门牌号成功");
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(HttpServletRequest request,Long id){
		doorplatService.delte(id);
		return Result.success("删除门牌号成功");
	}
}
